//
//  MNDRechargeResultViewController.m
//  minidai
//
//  Created by 容贤郁 on 2018/1/23.
//  Copyright © 2018年 迷你金服. All rights reserved.
//

#import "MNDRechargeResultViewController.h"

@interface MNDRechargeResultViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *resultImage;
@property (weak, nonatomic) IBOutlet UILabel *resultStatusLabel;
@property (weak, nonatomic) IBOutlet UILabel *resultDetailLabel;

@property (weak, nonatomic) IBOutlet UIView *failedView;

@property (weak, nonatomic) IBOutlet UIButton *rechargeButton;


@end

@implementation MNDRechargeResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.failedView setHidden:YES];
    
    if (self.rechargeResult.status == ResponseSuccess) {
        [self.failedView setHidden:YES];
        [self.resultImage setImage:[UIImage imageNamed:@"recharge_success"]];
        self.resultStatusLabel.text = @"充值成功";
        self.resultDetailLabel.text = [NSString stringWithFormat:@"您当前账户余额：￥%@", self.rechargeResult.data[@"user_amount_available"]];
    } else {
        [self.failedView setHidden:NO];
        [self.resultImage setImage:[UIImage imageNamed:@"recharge_failed"]];
        self.resultStatusLabel.text = @"充值失败";
        self.resultDetailLabel.text = self.rechargeResult.msg[@"error"];
    }
    
    self.rechargeButton.layer.borderColor = kColorOrange.CGColor;
    self.rechargeButton.layer.borderWidth = 1.0;
    
    [self initialBackBarButton];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initialBackBarButton {
    UIImage *backImage = [UIImage imageNamed:@"navi_back_btn_black"];
    
    //创建返回按钮
    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:backImage
                                                                          style:UIBarButtonItemStylePlain
                                                                         target:self
                                                                         action:@selector(backAction:)];
    //设置返回按钮的颜色
    [backBarButtonItem setTintColor:kColorBackButtonColor];
    
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                target:nil action:nil];
    //将宽度设为负值
    spaceItem.width = kScreenWidth < 414? -10 : -15;
    //将两个BarButtonItem都返回给NavigationItem
    if (@available(iOS 11.0, *)) {
        self.navigationItem.leftBarButtonItem = backBarButtonItem;
    } else {
        self.navigationItem.leftBarButtonItems = @[spaceItem, backBarButtonItem];
    }
}

- (void)backAction:(id)sender {
    [self backToRechargeViewController];
}

- (IBAction)rechargeAgainAction:(UIButton *)sender {
    [self backToRechargeViewController];
}

- (IBAction)tenderAction:(UIButton *)sender {
    
    if (self.isFromTender) {
        UIViewController *viewController = self.navigationController.viewControllers[2];
        [self.navigationController popToViewController:viewController animated:YES];
    } else {
        [self.tabBarController setSelectedIndex:1];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (IBAction)rechargeContinueAction:(UIButton *)sender {
    [self backToRechargeViewController];
}

- (void)backToRechargeViewController {
    UIViewController *viewController = self.navigationController.viewControllers[self.navigationController.viewControllers.count - 3];
    [self.navigationController popToViewController:viewController animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
